import info from './info'
import action from './action'

// 模拟H5设备。一般来自于login的模拟信息中，仅开发状态下生效
const moniH5 = ''

// 设备信息初始化
const initial = () => {
	// #ifdef MP-WEIXIN
	// const res = uni.getDeviceInfo()
	// #endif
	// #ifndef MP-WEIXIN
	// const res = uni.getSystemInfoSync()
	// #endif
	const res = uni.getSystemInfoSync()
	console.log('设备原始信息：')
	console.log(res)
	const {deviceId, language, system, platform, version, brand, model, pixelRatio, statusBarHeight, screenWidth, screenHeight, windowWidth, windowHeight, windowTop, windowBottom, safeArea, safeAreaInsets} = res
	// #ifdef H5
	info.terminal = 1
	// 判别是不是特殊设备上
	if (process.env.NODE_ENV === 'development' && moniH5) {
		info.apply_code = moniH5
	} else {
		const ua = navigator.userAgent.toLowerCase().match(/MicroMessenger/i)
		if (ua && ua[0]) {
			switch (ua[0]){
				case 'micromessenger':
					// 微信浏览器
					info.apply_code = 'wx_h5'
					break
				case 'AlipayClient':
					// 支付宝浏览器
					info.apply_code = 'ali'
					break
				default:
					info.apply_code = 'h5'
					break
			}
		} else {
			info.apply_code = 'h5'
		}
	}
	browserRedirect()
	// #endif
	// #ifdef APP-PLUS
	info.terminal = 2
	if (platform === 'ios') {
		info.apply_code = 'iOS'
	} else if (platform === 'android') {
		info.apply_code = 'Android'
	} else {
		info.apply_code = 'app'
	}
	// #endif
	// #ifdef MP-WEIXIN
	info.terminal = 3
	info.apply_code = 'wx_mp'
	// #endif
	// #ifndef APP-PLUS || H5 || MP-WEIXIN
	info.terminal = 4
	// #endif
	console.log('当前应用类型：' + info.apply_code)
	Object.keys(res).forEach(key => {
		info[key] = res[key]
	})
	
	if (process.env.NODE_ENV !== 'development') {
		// XR的真机结果竟然是0
		if (info.model === 'iPhone') { // platform === 'ios'
			if (safeAreaInsets.bottom === 0) {
				if (info.screenHeight >= 812) {
					safeAreaInsets.bottom = 34
				}
			}
		}
	}
	
	info.bottomHeight = safeAreaInsets.bottom
	info.navigationHeight = info.titleBarHeight + statusBarHeight
	info.mainHeight = windowHeight - safeAreaInsets.top - safeAreaInsets.bottom
	
	info.horizontal = action.GlobalMarginWidth()
	info.vertical = action.GlobalMarginHeight()
	info.fontSize = action.GlobalFont()
	info.mac = action.getMacAddress()
}

/**
 * 终端类型
 */
const DeviceType = {
	pc: 'pc',
	iPad: 'ipad',
	iPhone: 'iphone os',
	android: 'android',
	midp: 'midp',
	uc7: 'rv:1.2.3.4',
	uc: 'ucweb',
	ce: 'windows ce',
	wm: 'windows mobile',
}

/**
 * 判断终端浏览器类型
 */
const browserRedirect = () => {
    const sUserAgent = navigator.userAgent.toLowerCase()
	if (sUserAgent.indexOf(DeviceType.iPhone) > -1) {
		info.type = 'iPhone'
	} else {
		if (sUserAgent.indexOf(DeviceType.android) > -1) {
			info.type = 'android'
		} else {
			if (sUserAgent.indexOf(DeviceType.iPad) > -1) {
				info.type = 'iPad'
			} else {
				if (sUserAgent.indexOf(DeviceType.wm) > -1) {
					info.type = 'wm'
				} else {
					if (sUserAgent.indexOf(DeviceType.ce) > -1) {
						info.type = 'ce'
					} else {
						if (sUserAgent.indexOf(DeviceType.midp) > -1) {
							info.type = 'midp'
						} else {
							if (sUserAgent.indexOf(DeviceType.uc7) > -1) {
								info.type = 'uc7'
							} else {
								if (sUserAgent.indexOf(DeviceType.uc) > -1) {
									info.type = 'uc'
								} else {
									info.type = 'pc'
								}
							}
						}
					}
				}
			}
		}
	}
	console.log('当前终端浏览器类型：' + info.type)
}

module.exports = {
	info,
	initial
}